﻿using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace LD.Common
{
    public class TaskEx
    {
        public static Dictionary<string, TaskEx> TaskDict = new Dictionary<string, TaskEx>();
        private CancellationTokenSource tokenSource;
        private ManualResetEvent resetEvent = new ManualResetEvent(false);
        private string taskName;
        private bool pausing;
        public void Start(string name, Action action)
        {
            Step = 0;
            if (TaskDict.ContainsKey(name))
            {
                return;
            }
            taskName = name;
            tokenSource = new CancellationTokenSource();
            CancellationToken token = tokenSource.Token;
            var task = Task.Run(() =>
            {
                resetEvent.Set();
                while (true)
                {
                    //等待信号
                    resetEvent.WaitOne();
                    //取消
                    if (token.IsCancellationRequested)
                    {
                        Console.WriteLine("最后流程:" + Step);
                        return;
                    }

                    //方法
                    action?.Invoke();

                    Task.Delay(2).Wait();
                }
            }, token);

            TaskDict.Add(name, this);
        }
        public void Start(string name, Action<object> action, object obj)
        {
            Step = 0;
            if (TaskDict.ContainsKey(name))
            {
                return;
            }
            taskName = name;
            tokenSource = new CancellationTokenSource();
            CancellationToken token = tokenSource.Token;
            var task = Task.Run(() =>
            {
                resetEvent.Set();
                while (true)
                {
                    //等待信号
                    resetEvent.WaitOne();
                    //取消
                    if (token.IsCancellationRequested)
                    {
                        Console.WriteLine("最后流程:" + Step);
                        return;
                    }

                    //方法
                    action?.Invoke(obj);

                    Task.Delay(20).Wait();
                }
            }, token);

            TaskDict.Add(name, this);
        }
        private int step = 0;
        public int Step
        {
            get { return step; }
            set
            {
                LastStep = step;
                step = value;
            }
        }
        public int LastStep { get; private set; } = 0;
        /// <summary>
        /// 暂停
        /// </summary>
        public void Pause()
        {
            //暂停
            resetEvent.Reset();
            pausing = true;
        }
        /// <summary>
        /// 继续
        /// </summary>
        public void Reset()
        {
            //继续
            resetEvent.Set();
            pausing = false;
        }
        /// <summary>
        /// 关闭
        /// </summary>
        public void Cancel()
        {
            if (string.IsNullOrEmpty(taskName))
                return;
            tokenSource?.Cancel();

            if (pausing)
            {
                Reset();
                pausing = false;
            }
            //BcFactory.Instance.Control.StopALL();
            TaskDict.Remove(taskName);
            Console.WriteLine("【流程退出】" + taskName);
        }
        public static void Stop(string name)
        {
            if (TaskDict.ContainsKey(name))
            {
                var task = TaskDict[name];
                task?.Cancel();
            }
        }
    }
}
